package com.bwstudio.demo.provider.service;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.bwstudio.demo.provider.api.HelloService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class HelloServiceImpl implements HelloService {

	@Value("${spring.application.name}")
	public String appname;
	
	@Override
	@SentinelResource(value = "HelloService.hello", 
	blockHandler = "helloBlockHandler", 
	fallback = "helloFallback")
	public String hello(String str) {
		return str + "- From Provider Service: [" + appname + "]";
	}
	
	// Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
	public String helloBlockHandler(String str, BlockException ex) {
		log.warn("Oops: " + ex.getClass().getCanonicalName());
		return "HelloService.hello: 我被限流了; str: "+str;
	}

	// Fallback 函数，函数签名与原函数一致或加一个 Throwable 类型的参数.
	public String helloFallback(String str, Throwable ex) {
		log.error("Oops：" + ex.getClass().getCanonicalName());
		return "HelloService.hello: 我被降级了; str: "+str;
	}

}
